package com.pluszg.nutrition.product.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.pluszg.nutrition.api.controller.product.ProductController;
import com.pluszg.nutrition.common.resp.CommonResponse;
import com.pluszg.nutrition.model.product.Product;
import com.pluszg.nutrition.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
public class ProductControllerImpl implements ProductController {

    @Autowired
    private ProductService productService;

    @Override
    public CommonResponse<Product> add(@RequestBody Product product) {
        return new CommonResponse<>(productService.add(product));
    }

    @Override
    @SentinelResource(value = "getByCode")
    public CommonResponse<Product> findById(@PathVariable(value = "id") String id) {
        return new CommonResponse<>(productService.findById(id));
    }

    @Override
    @SentinelResource(value = "product/getByCode")
    public CommonResponse<List<Product>> List (@RequestBody Product product) {
        return new CommonResponse<>(productService.list(product));
        //throw new RuntimeException("error");
    }

    @Override
    public CommonResponse<Product> modify (@RequestBody Product product){
        return new CommonResponse<>(productService.modify(product));
    }

    @Override
    public CommonResponse<Boolean> delete (@RequestParam("id") String id){
        return new CommonResponse<>(productService.delete(id));
    }

    @Override
    public CommonResponse<Void> deduct (@RequestParam("id") String id,@RequestParam("deductCount") Integer deductCount){
        productService.deduct(id,deductCount);
        return new CommonResponse<>();
    }
}
